package com.scu.service;

import com.scu.dao.BlockDao;
import com.scu.dao.TopicDao;
import com.scu.pojo.CollectionTopic;
import com.scu.pojo.Report;
import com.scu.pojo.Topic;
import com.scu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TopicServiceImpl implements TopicService {

    @Autowired
    private TopicDao topicDao;

    @Autowired
    private BlockDao blockDao;

    /**
     * 查询帖子列表
     *
     * @param topic b_id/t_author_id/null
     * @return List<topic>
     */
    @Override
    public List<Topic> getTopicList(Topic topic) {
        return topicDao.getTopicList(topic);
    }


    @Override
    public List<Topic> getTopicCommentList(Topic topic) {
//        System.out.println("帖子及评论");
        System.out.println(topic);
        return topicDao.getTopicCommentList(topic);
    }

    //分页获取帖子信息
    @Override
    public List<Topic> getTopicListByPage(Topic topic, int page, int limit) {

        System.out.println("要找的t_title");
        System.out.println(topic.getT_title());
        System.out.println("over");
        return topicDao.getTopicListByPage((page - 1) * limit, limit, topic);
    }

    //分页获取精华帖
    @Override
    public List<Topic> getEliteTopicListByPage(Topic topic, int page, int limit) {
        //        (page-1)*limit,limit,topic
        topic.setT_elite_status("1");
        return topicDao.getTopicListByPage((page - 1) * limit, limit, topic);
    }

    //获取我的帖子
    @Override
    public List<Topic> getMyTopicList(Topic topic) {
        return topicDao.getMyTopicList(topic);
    }

    //分页获取我的帖子
    @Override
    public List<Topic> getMyTopicListByPage(Topic topic, int page, int limit) {
        //        (page-1)*limit,limit,topic
        return topicDao.getMyTopicListByPage((page - 1) * limit, limit, topic);
    }

    //获取我的收藏
    @Override
    public List<Topic> getMyCollectionTopicList(Topic topic) {
        return topicDao.getMyCollectionTopicList(topic);
    }

    //分页获取我的收藏
    @Override
    public List<Topic> getMyCollectionTopicListByPage(Topic topic, int page, int limit) {
        return topicDao.getMyCollectionTopicListByPage((page - 1) * limit, limit, topic);
    }

    @Override
    public void approvalFile(MultipartFile filecontent, String realPath) {
        OutputStream os = null;
        InputStream inputStream = null;
        String fileName = null;
        try {
            inputStream = filecontent.getInputStream();
            fileName = filecontent.getOriginalFilename();
            System.out.println("fileName=" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String path = realPath + "/static/img/topicPicture";
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询当前帖子评论数
     *
     * @param topic t_id
     * @return int comment_num
     */
    @Override
    public int getCommentNum(Topic topic) {

        return topicDao.getCommentNum(topic);
    }

    /**
     * 发帖子
     *
     * @param topic t_title t_type t_author_id b_id
     * @return
     */
    @Override
    public int postTopic(Topic topic) {

        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // new Date()为获取当前系统时间
        topic.setT_date(df.format(new Date()));
        System.out.println(topic.getB_id());
        blockDao.addTopicNum(topic.getB_id());

        return topicDao.postTopic(topic);
    }

    /**
     * 修改帖子
     *
     * @param topic t_titlt b_id
     * @return
     */
    @Override
    public int redactTopic(Topic topic) {

        return topicDao.redactTopic(topic);
    }

    /**
     * 获取返回发布文章数目最多的10名用户
     *
     * @return
     */
    @Override
    public List<User> getPostMostUser() {

        return topicDao.getPostMostUser();
    }

    /**
     * 删除帖子
     *
     * @param topic t_id
     * @return
     */
    @Override
    public int removeTopicById(Topic topic) {
        topic.setT_delete_status("1");
        //Mybatis不支持 布尔类型
        System.out.println("找到的b_id");
        System.out.println(topicDao.findB_idByT_id(topic.getT_id()));
        blockDao.subTopicNum(topicDao.findB_idByT_id(topic.getT_id()));
        return topicDao.alterStatusTopic(topic);
    }

    /**
     * 恢复帖子
     *
     * @param topic
     * @return
     */
    @Override
    public int recoverTopicById(Topic topic) {

        topic.setT_delete_status("0");
        return topicDao.alterStatusTopic(topic);
    }

    /**
     * 置顶帖子
     *
     * @param topic
     * @return
     */
    @Override
    public int setTopTopicById(Topic topic) {
        topic.setT_top_status("1");

        return topicDao.alterStatusTopic(topic);
    }

    /**
     * 解除置顶
     *
     * @param topic
     * @return
     */
    @Override
    public int removeTopTopicById(Topic topic) {
        topic.setT_top_status("0");

        return topicDao.alterStatusTopic(topic);

    }

    /**
     * 帖子加精
     *
     * @param topic
     * @return
     */
    @Override
    public int setEliteTopicById(Topic topic) {
        topic.setT_elite_status("1");

        return topicDao.alterStatusTopic(topic);
    }

    /**
     * 取消加精
     *
     * @param topic
     * @return
     */
    @Override
    public int removeEliteTopicById(Topic topic) {
        topic.setT_elite_status("0");

        return topicDao.alterStatusTopic(topic);
    }

    /**
     * 关闭帖子
     *
     * @param topic
     * @return
     */
    @Override
    public int closeTopicById(Topic topic) {
        topic.setT_close_status("1");
        return topicDao.alterStatusTopic(topic);
    }

    /**
     * 打开帖子
     *
     * @param topic
     * @return
     */
    @Override
    public int reopenTopicById(Topic topic) {
        topic.setT_close_status("0");
        return topicDao.alterStatusTopic(topic);
    }

    //举报帖子
    @Override
    public int informTopic(Report report) {
        return topicDao.informTopic(report);
    }

    /**
     * 检查该用户是否收藏该贴
     *
     * @param CollectionTopic
     * @return
     */
    @Override
    public int checkCollectTopic(CollectionTopic CollectionTopic) {
        List<String> result = topicDao.checkCollectTopic(CollectionTopic);
        System.out.println(result);
        if (result.size() > 0) {
            System.out.println("已收藏");
            return 1;
        } else {
            System.out.println("未收藏");
            return 0;
        }
    }

    //收藏帖子
    @Override
    public int collectTopic(CollectionTopic collectionTopic) {
        return topicDao.collectTopic(collectionTopic);
    }

    //取消收藏
    @Override
    public int deleteCollectionTopic(CollectionTopic CollectionTopic) {
        return topicDao.deleteCollectionTopic(CollectionTopic);
    }

    @Override
    public List<Topic> getTopicByID(int t_id) {
        return topicDao.getTopicByID(t_id);
    }

    @Override
    public void deleteDir(String dirPath) {
        File file = new File(dirPath);// 读取
        if (file.isFile()) { // 判断是否是文件夹
            file.delete();// 删除
        } else {
            File[] files = file.listFiles(); // 获取文件
            if (files == null) {
                file.delete();// 删除
            } else {
                for (int i = 0; i < files.length; i++) {// 循环
                    deleteDir(files[i].getAbsolutePath());
                }
                file.delete();// 删除
            }
        }
    }

}
